package com.bite.writtenTraining;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Date1101 {
    /**
     * 算法题：数组中两个字符串最小距离
     * https://www.nowcoder.com/questionTerminal/2c6a0a8e1d20492f92941400036e0890
     * @param args
     * @throws Throwable
     */
    //更新算法
    public static void main(String[] args) throws Throwable {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        String[] str = reader.readLine().split(" ");
        String str1 = str[0];
        String str2 = str[1];
        int prev1 = -1, prev2 = -1;
        int ret = -1;
        for(int i = 0; i < n; i++){
            String s = reader.readLine();
            if(s.equals(str1)){
                if(prev2 != -1){
                    ret = i - prev2;
                }
                prev1 = i;
            }
            if(s.equals(str2)){
                if(prev1 != -1){
                    ret = i - prev1;
                }
                prev2 = i;
            }
        }
        System.out.println(ret);
    }
}
